<?php

/**
 * Description of Catalog
 *
 * @author Kam
 */
class Application_Model_DAO_Adv extends My_DAO_Abstract {

    const DB_ADV = 'Catalog';
    const TYPE_IMG = 'image';
    const TYPE_HTML = 'html';
    const PLACE_TOP = 'top';
    const PLACE_LEFT = 'left';
    const PLACE_RIGHT = 'right';
    const PLACE_BOTTOM = 'bottom';
    const IMG_PREFIX = 'img:';
    
    public function increase($id, $interestColumn, $column = 'adv_id') {
        $data = array(
            $interestColumn => new Zend_Db_Expr($interestColumn .' + 1'),
        );
        return $this->getDbTable()->update($data, $id, $column);
    }

    public function getAll() {
        $select = $this->getDbTable()->select()
                ->order(array('creation_date DESC'));
        return $select;
    }

    public function deactivate($id, $column = 'code') {
        return $this->getDbTable()->update(array('admin_active' => 0), $id, $column);
    }

    public function activate($id, $column = 'code') {
        return $this->getDbTable()->update(array('admin_active' => 1), $id, $column);
    }

    /**
     * @return array
     */
    public function getRandomAdvsByPlace($place, $limit) {
        $select = $this->getDbTable()->select()
                ->from(array('a' => 'adv'))
                // join po obrazku dla wydajności -> img_code
                ->where('a.place = ?', $place)
                ->where('a.admin_active = 1')
                ->order(array(new Zend_Db_Expr('RAND()')))
                ->limit($limit)
        ;
        $result = $select->query()->fetchAll();
        if (empty($result)) {
            return null;
        }
        $ids = array();
        foreach ($result as $item) {
            $ids[] = $item['adv_id'];
        }
        $where = $this->quoteInto('adv_id IN (?)', $ids);
        $this->getDbTable()->update(array(
            'displayed_cnt' => new Zend_Db_Expr('displayed_cnt + 1'),
                ), $where);

        if ($limit == 1) {
            return $result[0];
        }
        return $result;
    }

    public function update(array $data, $id, $column = 'code') {
        $record = array();
        $this->setSafety($record, $data, 'name');
        $this->setSafety($record, $data, 'place');
        $this->setSafety($record, $data, 'type');
        $this->setSafety($record, $data, 'content');
        $this->setSafety($record, $data, 'url');
    }

    public function addNew($data) {
        $record = array();
        $this->setSafety($record, $data, 'name');
        $this->setSafety($record, $data, 'place');
        $this->setSafety($record, $data, 'type');
        $this->setSafety($record, $data, 'content');
        $this->setSafety($record, $data, 'url');

        $recordId = $this->getDbTable()->insert($record);

        $record = $this->getDbTable()->get($recordId);
        return $record;
    }

    protected function getDbTableNameMap() {
        return array(
            self::DB_ADV => 'Application_Model_DbTable_Adv',
        );
    }

    protected function getDefaultDbTableName() {
        return self::DB_ADV;
    }

}
